<?php
/**
 * 【培训控制器】
 * Created by PhpStorm.
 * User: 孙凯
 * Date: 2018-06-07
 * Time: 15:20
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Tymon\JWTAuth\Exceptions\JWTException;
use Illuminate\Support\Facades\DB;

class TrainController extends Controller
{
    /**
     * 【司机培训列表index】
     * @Author sk
     * @DateTime 2018-06-07
     * @param Request $request
     * @return array
     */
    public function index(Request $request)
    {
        $array = $request->all();
        if (isset($array['s'])) {
            unset($array['s']);
        }
        $select = [
            'train.id',
            'train.class_name',//培训名称
            'train.class_date',//培训日期
            'train.start',//开始日期
            'train.end',//结束日期
            'train.train_date',//培训时长
            'train.number',//驾驶证号
            'train.driver_id',
            'train.user_admin',//修改人
            'train.update_time',//培训跟新时间
            'driver.driver_name',//司机姓名
            'driver.driver_phone',//司机手机号
            'driver.sex',//性别 :1男 2女
            'driver.convoy_id',//车队id
            'driver.car_id',//车辆id
            'driver.licenseId',//驾驶证编号
            'car_car.num',//车牌号
            'car_car.typeid',//车系id
            'car_series.name as sname',//车系名称
            'convoy.name',//车队名称
            'convoy.branch_id',
            'branch.company_name',//机构名称
            'branch.type'//机构类型
        ];
        $handle = DB::table('train')
            ->leftjoin('driver', 'train.driver_id', '=', 'driver.id')//差司机
            ->leftjoin('convoy', 'driver.convoy_id', '=', 'convoy.id')//车队
            ->leftjoin('branch', 'convoy.branch_id', '=', 'branch.id')//机构
            ->leftjoin('car_car', 'driver.car_id', '=', 'car_car.id')//车牌号
            ->leftjoin('car_series', 'car_car.typeid', '=', 'car_series.id')//车系
            ->select($select)
            ->orderBy('train.update_time', 'desc');
        foreach ($array as $key => $val) {
            $array[$key] && $handle->where($key, 'like', '%' . $val . '%');
        }
        $datas = $handle->get();
        $data = json_decode(json_encode($datas), true);
        foreach ($data as $k => &$v) {
            $v['train_date'] = ceil(($v['start'] - $v['end']) / 60);//培训时长
        }
        foreach ($data as $k => &$v) {
            $v['sex'] = $v['sex'] = 1 ? '男' : '女';
            $v['start'] = date('Y-m-d H:i:s', $v['start']);
            $v['end'] = date('Y-m-d H:i:s', $v['end']);
        }
        // dd($datas);
        return $data ? response(['data' => $data, 'code' => 1000, 'message' => '信息返回成功！'], 200) : response(['data' => '', 'code' => 1011, 'message' => '信息返回失败！'], 400);


    }

    /**
     * 【查询公司id和车队id】
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function create()
    {
        $hht = Db::table('driver')
            ->join('convoy', 'driver.convoy_id', '=', 'convoy.id')//车队
            ->join('branch', 'convoy.branch_id', '=', 'branch.id');//机构;
        $name = $hht->select('name', 'convoy.id as convoy_id')->distinct()->get();
        $company_name = $hht->select('company_name', 'branch.id as branch_id ')->get();
        $name = json_decode(json_encode($name), true);
        $company_name = json_decode(json_encode($company_name), true);
        return $name ? response(['name' => $name, 'company_name' => $company_name, 'message' => '成功', 'code' => 1000], 200) : response(['msg' => '', 'message' => '失败', 'code' => 1011], 400);
    }

    /**
     * 【展示司机列表】
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function showadd(Request $request)
    {
        $array = $request->all();
        if (isset($array['s'])) {
            unset($array['s']);
        }
        if ($request['branch_id']) {
//根据公司id查询
            $branch = DB::table('convoy')->where('convoy.branch_id', $request['branch_id'])
                ->join('driver', 'driver.convoy_id', '=', 'convoy.id');
            $bb = $branch->get();
            $msg = json_decode(json_encode($bb), true);
            foreach ($msg as $k => &$v) {
                $v['sex'] = $v['sex'] = 1 ? '男' : '女';
            }
            return $msg ? response(['msg' => $msg,
                'message' => '成功', 'code' => 1000], 200) : response(['msg' => '', 'message' => '失败', 'code' => 1011], 400);
        } elseif ($request['convoy_id']) {
//根据车队对查询
            $convoy = DB::table('driver')->where('driver.convoy_id', $request['convoy_id']);
            $cc = $convoy->get();
            $msg = json_decode(json_encode($cc), true);
            foreach ($msg as $k => &$v) {
                $v['sex'] = $v['sex'] = 1 ? '男' : '女';
            }
            //dd($msg);
            return $msg ? response(['msg' => $msg,
                'message' => '成功', 'code' => 1000], 200) : response(['msg' => '', 'message' => '失败', 'code' => 1011], 400);
        } else {
            $handle = Db::table('driver')
                ->join('convoy', 'driver.convoy_id', '=', 'convoy.id')//车队
                ->join('branch', 'convoy.branch_id', '=', 'branch.id');//机构;
            foreach ($array as $key => $val) {
                $array[$key] && $handle->where($key, 'like', '%' . $val . '%');
            }
            $datas = $handle->get();
        }
        $msg = json_decode(json_encode($datas), true);
        foreach ($msg as $k => &$v) {
            $v['sex'] = $v['sex'] = 1 ? '男' : '女';
        }
        return $msg ? response(['msg' => $msg,
            'message' => '成功', 'code' => 1000], 200) : response(['msg' => '', 'message' => '失败', 'code' => 1011], 400);
    }


    /**
     * 【司机培训列表添加add】
     * @Author sk
     * @DateTime 2018-06-07
     * @type  int 1添加
     * @param Request $request
     * @return array
     */
    public function add(Request $request)
    {
        $data = $request->all();
        if (isset($data['s'])) {
            unset($data['s']);
        }
        if (empty($data['driver_id'])) {
            return 'driver_id为空';
        }
        $data['start'] = strtotime($data['start']);
        $data['end'] = strtotime($data['end']);
        try {
            $msg = Db::table('train')->insert($data);
            $msg = json_encode($msg);
            return $msg ? response(['msg' => $msg, 'message' => '添加成功', 'code' => 1000], 200) : response(['msg' => '', 'message' => '添加失败', 'code' => 1011], 400);
        } catch (JWTException $e) {
            return response(['msg' => '', 'code' => 1011, 'message' => '非法的token']);
        }
    }

    /**
     * 【修改司机方法展示司机信息】
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function show(Request $request)
    {
        $array = $request->all();
        if (isset($array['s'])) {
            unset($array['s']);
        }
        if (empty($array['id'])) {
            return 'id不能为空';
        }
        $handle = Db::table('train')
            ->leftjoin('driver', 'train.driver_id', '=', 'driver.id')
            ->where('train.id', $array['id'])
            ->select('train.*', 'driver.attendant', 'driver.sex', 'driver.driver_phone', 'driver.driver_name', 'driver.role');//
        $datas = $handle->first();

        $msg = json_decode(json_encode($datas), true);
        // 1载客，2接单，3空驶，4停运，5休假，6离职
        // dd($msg);
        $msg['sex'] = $msg['sex'] = 1 ? '男' : '女';
        $msg['start'] = date('Y-m-d H:i:s', $msg['start']);
        $msg['end'] = date('Y-m-d H:i:s', $msg['end']);
        $msg['role'] = $msg['role'] = 1 ? '司机' : '队长';
        switch ($msg['attendant']) {
            case 1:
                $msg['attendant'] = '载客';
                break;
            case 2:
                $msg['attendant'] = '接单';
                break;
            case 3:
                $msg['attendant'] = '空驶';
                break;
            case 4:
                $msg['attendant'] = '停运';
                break;
            case 5:
                $msg['attendant'] = '休假';
                break;
            case 6:
                $msg['attendant'] = '离职';
                break;
        }
        return $msg ? response(['msg' => $msg, 'message' => '成功', 'code' => 1000], 200) : response(['msg' => '', 'message' => '暂无数据', 'code' => 1011], 400);
    }

    /**
     * 【司机培训列表修改】
     * @Author sk
     * @DateTime 2018-06-07
     * @param driver_id int 司机id
     * @param Request $request
     * @return array
     */
    public function save(Request $request)
    {
        $data = $request->all();
        if (isset($data['s'])) {
            unset($data['s']);
        }
        if (empty($data['id'])) {
            return 'id为空';
        }
        $data['start'] = strtotime($data['start']);
        $data['end'] = strtotime($data['end']);
        try {
            $id = $data['id'];
            $msg = Db::table('train')->where('id', $id)->update($data);
            return $msg ? response(['msg' => $msg, 'message' => '修改成功', 'code' => 1000], 200) : response(['msg' => '', 'message' => '修改失败', 'code' => 1011], 400);
        } catch (JWTException $e) {
            return response(['msg' => '', 'code' => 1011, 'message' => '非法的token']);
        }


    }


    /**
     * 【司机培训列表删除del】
     * @Author sk
     * @DateTime 2018-06-07
     * @param driver_id int 司机id
     * @param Request $request
     * @return array
     */
    public function del(Request $request)
    {
        $id = $request->only('id');
        try {
            $msg = Db::table('train')->where('id', $id)->delete();
            return $msg ? response(['msg' => $msg, 'message' => '删除成功', 'code' => 1000], 200) : response(['msg' => '', 'message' => '删除失败', 'code' => 1011], 400);
        } catch (JWTException $e) {
            return response(['msg' => '', 'code' => 1011, 'message' => '非法的token']);
        }
    }
}